package com.xm.xmapp.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.amap.api.maps.AMapUtils;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.map.ClusterItem;
import com.xm.xmapp.tools.StringTools;

public class HCDialog extends Dialog {

	private Context context;
	private CharSequence title_str;
	private CharSequence queding_str;
	private OnClickListener l;
	private ClusterItem item;

	public static interface OnClickListener {
		public void onClick(ClusterItem item);
	}


	public HCDialog(Context context, CharSequence title, ClusterItem item,
			CharSequence queding,OnClickListener l) {
		super(context, R.style.Dialog);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.title_str = title;
		this.item = item;
		this.queding_str = queding;
		this.l = l;

	}

	@SuppressLint("InflateParams")
	public void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_tip_one, null);
		TextView title = (TextView) view.findViewById(R.id.title);
		TextView content = (TextView) view.findViewById(R.id.content);

		TextView queding = (TextView) view.findViewById(R.id.queding);

		queding.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				HCDialog.this.dismiss();
			}
		});
		if (queding_str != null)
			queding.setText(queding_str);

		title.setText(title_str == null ? "" : title_str);

		if (item != null) {

			// 车辆此时的位置不在还车区域内，距离最近的还车地点(%@)还有%@，导航去这里？

			float min = AMapUtils.calculateLineDistance(MyAppcation
					.getMyAppcation().getMyLocation(), item.getLatLng());

			String str1 = StringTools.getJuliString(min, 1000);

			if (min > 1000) {
				str1 = "车辆此时的位置不在还车区域内，距离最近的还车地点(" + item.getName() + ")还有"
						+ str1 + "km，";
			} else {
				str1 = "车辆此时的位置不在还车区域内，距离最近的还车地点(" + item.getName() + ")还有"
						+ str1 + "m，";
			}

			String str2 = "导航去这里？";

			content.setHighlightColor(Color.parseColor("#00000000"));
			SpannableString spanableInfo = new SpannableString(str1+str2);
			spanableInfo.setSpan(new Clickable(l, item), str1.length(), str1.length()+str2.length(),
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			content.setText(spanableInfo);
			content.setMovementMethod(LinkMovementMethod.getInstance());

		}

		setContentView(view);
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
		dialogWindow.setAttributes(lp);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		init();

	}
}
